package com.djl.mvc.controller;

import com.djl.crowd.entity.Auth;
import com.djl.crowd.entity.Role;
import com.djl.crowd.util.ResultEntity;
import com.djl.service.AdminService;
import com.djl.service.AuthService;
import com.djl.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author le
 * @Date 2021/11/29 20:57
 * @Version 1.0
 */
@Controller
public class AssignController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AuthService authService;

    @PreAuthorize("hasRole('programmer') or hasRole('总经理')")
    @RequestMapping("/assign/do/role/assign/auth.json")
    @ResponseBody
    public ResultEntity<String> saveRoleAuthRelationShip(
            @RequestBody Map<String, List<Integer>> map) {

        authService.saveRoleAuthRelationShip(map);

        return ResultEntity.successWithoutData();

    }


    @RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
    @ResponseBody
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId) {
        List<Integer> assignedAuthIds = authService.queryAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(assignedAuthIds);
    }


    @PostMapping("/assign/get/all/auth.json")
    @ResponseBody
    public ResultEntity<List<Auth>> getAllAuth() {
        List<Auth> auths = authService.selectAll();
        return ResultEntity.successWithData(auths);
    }

    @PreAuthorize("hasRole('programmer') or hasRole('总经理')")
    @PostMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship(@RequestParam("adminId") Integer adminId,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("keyword") String keyword,
                                            // required=false 表示这个请求参数不是必须的
                                            @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList) {

        adminService.saveAdminRoleRelationship(roleIdList, adminId);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    @RequestMapping("assign/to/assign/role/page.html")
    public String toAssignRolePage(@RequestParam("adminId") Integer adminId, Model model) {
        // 查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);
        // 查询未分配角色
        List<Role> unAssignedRoleList = roleService.getUnAssignRole(adminId);

        model.addAttribute("assignedRoleList", assignedRoleList);
        model.addAttribute("unAssignedRoleList", unAssignedRoleList);
        return "assign-role";
    }


}
